﻿using System.Collections.Generic;
using DataLayer.Repo;
using DataLayer.Types;
using StructureMap;

namespace ViewModel.Finance
{
    public class BudgetItemViewModel : ItemPropertiesViewModel<BudgetItem>
    {
        private string note;
        private decimal amount;
        private bool active;
        private FrequencyItem frequency;
        private CategoryItem category;
        private BalanceType balanceType;

        /// <summary>
        /// cTor
        /// </summary>
        /// <param name="budgetItem"></param>
        public BudgetItemViewModel(BudgetItem budgetItem) : base(budgetItem)
        {
            LoadData();
        }

        private void LoadData()
        {
            name = Item.Name;
            note = Item.Note;
            amount = Item.Amount;
            active = Item.Active;
            frequency = Item.Frequency;
            category = Item.Category;
            balanceType = Item.BalanceType;
        }

        public string Note {
            get { return note; } 
            set { note = value; OnPropertyChanged("Note");} 
        }

        public decimal Amount
        {
            get { return amount; }
            set { amount = value; OnPropertyChanged("Amount"); }
        }

        public bool Active 
        {
            get { return active; }
            set { active = value; OnPropertyChanged("Active"); }
        }

        public FrequencyItem Frequency
        {
            get { return frequency; }
            set { frequency = value; OnPropertyChanged("Frequency"); }
        }

        public CategoryItem Category
        {
            get { return category; }
            set { category = value; OnPropertyChanged("Category"); }
        }

        public BalanceType BalanceType
        {
            get { return balanceType; }
            set { balanceType = value; OnPropertyChanged("BalanceType"); }
        }

        public ICollection<FrequencyItem> FrequencyItems
        {
            get { return ObjectFactory.GetInstance<IRepository<FrequencyItem>>().FindAll(); }
        }

        public ICollection<CategoryItem> CategoryItems
        {
            get { return ObjectFactory.GetInstance<IRepository<CategoryItem>>().FindAll(); }
        }

        public ICollection<BalanceType> BalanceTypes
        {
            get { return ObjectFactory.GetInstance<IRepository<BalanceType>>().FindAll(); }
        }

        private void CollectData()
        {
            Item.Name = name;
            Item.Note = note;
            Item.Amount = amount;
            Item.Active = active;
            Item.Frequency = frequency;
            Item.Category = category;
            Item.BalanceType = BalanceType;
        }


        /// <summary>
        /// Collects the form data into the BudgetItem object and then saves it to the database.
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            CollectData();
            Item.Save();
            return true;
        }
    }
}
